package com.daoliuhe.scaffold.web.controller.admin;

import com.daoliuhe.scaffold.model.ListEntry;
import com.daoliuhe.scaffold.model.ListName;
import com.daoliuhe.scaffold.service.ListEntryService;
import com.daoliuhe.scaffold.service.ListNameService;
import com.daoliuhe.scaffold.tools.Constants;
import com.daoliuhe.scaffold.web.dto.ListNameDto;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/listName")
public class ListNameController {

    private static final Logger logger = LoggerFactory.getLogger(ListNameController.class);
    @Autowired
    ListNameService listNameService;

    @Autowired
    ListEntryService listEntryService;

    @RequestMapping("/list")
    public ModelAndView list(ListName entity) throws UnsupportedEncodingException {
        logger.info("list,entity:{}", entity);
        ModelAndView mav = new ModelAndView("admin/listName/list");
        mav.addAllObjects(listNameService.getPageData(entity));
        mav.addObject("entity", entity);
        return mav;
    }

    @RequestMapping("/edit")
    public ModelAndView edit(ListName entity, @RequestParam(required = false) String tab, ListNameDto listNameDto) {
        logger.info("edit,entity:{},tab:{},listNameDto{}", entity, tab, listNameDto);
        ModelAndView mav = new ModelAndView("admin/listName/edit");
        if (null == listNameDto || listNameDto.getsPage() == 0 || listNameDto.getsRows() == 0) {
            listNameDto = new ListNameDto(1, 10);
        }
        if (!StringUtils.isEmpty(entity.getId())) {
            ListName listName = listNameService.getById(entity.getId());
            listName.setPage(entity.getPage());
            listName.setRows(entity.getRows());
            mav.addObject("entity", listName);
            mav.addObject("listNameDto", listNameDto);
            //只有该列表是普通列表（listType为0），才去查其列表项
            if (!StringUtils.isEmpty(listName) && Constants.LIST_ORDINARY.equals(listName.getListType())) {
                ListEntry le = new ListEntry();
                le.setListId(entity.getId());
                le.setListType(listName.getListType());
                le.setPage(listNameDto.getsPage());
                le.setRows(listNameDto.getsRows());
                le.setEntryValue(listNameDto.getEntryValue());
                le.setEnabled(listNameDto.getListValueEnabled());
                le.setDescription(listNameDto.getDesc());
                mav.addObject("simpleListMap", listEntryService.getPageData(le));
                mav.addObject("tab", tab);
            }
        }
        mav.addObject("param", entity);
        return mav;
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(ListName entity) {
        logger.info("saveOrUpdate,entity:{}", entity);
        listNameService.saveOrUpdate(entity);
        return "redirect:/listName/list";
    }

    @RequestMapping("/data")
    @ResponseBody
    public Object data(ListName entity) {
        logger.info("data,entity:{}", entity);
        return listNameService.getPageData(entity);
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(ListName entity) {
        logger.info("listData,entity:{}", entity);
        entity.setEnabled(Constants.ENABLE);
        //entity.setListType(Constants.LIST_ORDINARY);
        return listNameService.getPageData(entity);
    }

    @RequestMapping("/vApiName")
    @ResponseBody
    public boolean vApiName(ListName entity) {
        logger.info("vApiName,entity:{}", entity);
        return listNameService.vApiName(entity);
    }

}
